package com.rongx.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rongx.reggie.common.R;
import com.rongx.reggie.entity.Category;
import com.rongx.reggie.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@RequiredArgsConstructor  // 为 final 字段生成构造器

public class CategoryController {

    private final CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("save category: {}", category);
        categoryService.save(category);
        return R.success("新增分类success");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<Category>> page(int page, int pageSize) {
        return R.success(categoryService.setPage(page, pageSize));
    }

    @DeleteMapping
    public R<String> delete(Long ids) {
        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }

    /**
     * 根据id修改分类信息
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类信息：{}", category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) {
        return R.success(file.getName());
    }

    /**
     * 根据条件查询分类数据
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        return R.success(categoryService.getList(category));
    }
}
